// 数据列表
const list = document.querySelector('.list')

// 控件栏DOM对象
const first = document.querySelector('.first')
const last = document.querySelector('.last')
const next = document.querySelector('.next')
const theLast = document.querySelector('.theLast')
const paged = document.querySelector('.paged')
// 页码总数DOM对象
const pageSum = document.querySelector('.controlTable select')
const allPage = document.querySelector('.allPage')

const UID = localStorage.getItem('UID')   //获取当前登录ID

// 页码
let page = 1
let pageNumber = 5
// 记录更新前的院系号
let oldProfessMajorId
// 记录当前页数据行数量
let trNumber
// 记录最大页数
let maxPage = 0

async function getCourseGrade() {
  const result = (await axios({
    url: 'http://127.0.0.1:8080/Student/GradeInquiry/getCourseGrade',
    params: {
      UID,
      page,
      pageNumber
    }
  })).data.data
  const htmlStr = await Promise.all(result.map(async (e) => {
    // 获取教师名字
    const name = (await axios({
      url: 'http://127.0.0.1:8080/Teacher/CourseManagement/getTeacherNameById',
      params: {
        UID: e.teacher
      }
    })).data.data
    // 获取绩点和学分绩点
    const CPAStr = (await axios({
      url: 'http://127.0.0.1:8080/Student/GradeInquiry/getCPA',
      params: {
        studentId: UID,
        classId: e.id
      }
    })).data.data
    console.log(CPAStr);
    return `
    <tr data-id="${e.id}">
      <td>${e.id}</td>
      <td>${e.name}</td>
      <td>${e.credit}</td>
      <td>${name}</td>
      <td>${e.location}</td>
      <td>${e.instructionalMethod}</td>
      <td>${e.degreeDemand}</td>
      <td>${CPAStr.CPA}</td>
      <td>${CPAStr.GPA}</td>
    </tr>
    `
  }))
  list.innerHTML = htmlStr.join('')
}

getCourseGrade()

async function getPageSum() {
  const result = (await axios({
    url: 'http://127.0.0.1:8080/Student/GradeInquiry/getPageSum',
    params: {
      id: UID,
      pageNumber
    }
  })).data.data
  allPage.innerHTML = result
}

getPageSum()    //获取最大页数

// 表格控件监听
first.addEventListener('click', () => {
  page = 1
  getCourseGrade()
})

last.addEventListener('click', () => {
  if (page > 1) {
    --page
    getCourseGrade()
  }
})

next.addEventListener('click', () => {
  getMaxPage()
  if (page < maxPage) {
    ++page
    getCourseGrade()
  }
})

theLast.addEventListener('click', () => {
  page = maxPage
  getCourseGrade()
})

pageSum.addEventListener('change', () => {
  pageNumber = pageSum.value
  page = 1
  getCourseGrade()
})

paged.addEventListener('change', () => {
  page = paged.value
  // 限定page范围
  page = page > maxPage ? maxPage : page
  page = page < 1 ? 1 : page
  getCourseGrade()
})